package com.handpay.zztong.hp.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.zztong.hp.R;

/**
 * 
 * @author whyuan 自定义的dialog
 */
public class KalefuDialog extends Dialog {

	// 先调用构造方法在调用oncreate方法

	private static int i = 1;
	static KalefuDialog dialog;
	static boolean isRun = true;

	// private MyDialog myDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public KalefuDialog(Context context) {

		super(context);
	}

	public KalefuDialog(Context context, int theme) {
		super(context, theme);
	}

	@Override
	public void show() {
		super.show();
	}

	public static class Builder {

		private Context context;
		private String str = null;
		private int imageId = 0;
		private int screenWidth ;
		private int screenHeight;
		
		// private TextView msg=(TextView)findViewById(R.id.message);
		public Builder(Context context ,String str,int imageId) {
			this.context = context;
			this.str = str;
			this.imageId = imageId;
		}

		public KalefuDialog show() {
			isRun = true;
			KalefuDialog dialog = create();
			Window window = dialog.getWindow();
			WindowManager.LayoutParams lp = window.getAttributes();
			lp.dimAmount = 0f;
			window.setAttributes(lp);
			dialog.show();
			return dialog;
		}

		public KalefuDialog create() {
			screenWidth = context.getResources().getDisplayMetrics().widthPixels;
			screenHeight = context.getResources().getDisplayMetrics().heightPixels;
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final Handler myHandler;
			// instantiate the dialog with the custom Theme
			dialog = new KalefuDialog(context, R.style.HPDialog);
			dialog.setCancelable(false);
			dialog.setCanceledOnTouchOutside(false);// android
													// 4.0以上dialog点击其他地方也会消失false以后就只能点击按钮消失

			View layout = inflater.inflate(R.layout.kalefudialog, null);
			final TextView tv = (TextView) layout.findViewById(R.id.tv_kalefu);
			final TextView tvStr = (TextView) layout.findViewById(R.id.tv);
			final ImageView iv = (ImageView) layout.findViewById(R.id.ivImage);
			tvStr.setText(str);
			iv.setBackgroundResource(imageId);
			
			myHandler = new Handler() {
				public void handleMessage(android.os.Message msg) {
					if (msg.what == 1) {
						tv.setText(".");
					}
					if (msg.what == 2) {
						tv.setText("..");
					}
					if (msg.what == 3) {
						tv.setText("...");
					}
					super.handleMessage(msg);
				}

			};

			new Thread(new Runnable() {

				@Override
				public void run() {
					while (isRun) {
						try {
							Thread.sleep(500);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						i++;

						if (i > 3) {
							i = 1;
						}
						Message msg = new Message();
						msg.what = i;
						myHandler.sendMessage(msg);
					}
				}

			}).start();

			int width = context.getResources().getDrawable(R.drawable.note_top)
					.getIntrinsicWidth();
			
			layout.findViewById(R.id.rlDialog).getLayoutParams().width = (int) (screenWidth * 0.55);
			layout.findViewById(R.id.rlDialog).getLayoutParams().height = (int) (screenWidth * 0.6 * 0.8);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			// set the dialog title

			dialog.setContentView(layout);
			return dialog;
		}
	}

	/**
	 * 关闭
	 */
	public void close() {
		dialog.dismiss();
		isRun = false;
	}
	@Override
	public void cancel() {
		super.cancel();
		close();
	}

}
